//
//  XMGTopTopicView.m
//  BuDeJie19
//
//  Created by hjj on 2016/12/3.
//  Copyright © 2016年 hjjuny. All rights reserved.
//

#import "XMGTopTopicView.h"
#import <UIImageView+WebCache.h>
#import "XMGTopicItem.h"
#import "NSDate+Date.h"
@interface XMGTopTopicView ()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *nameView;
@property (weak, nonatomic) IBOutlet UILabel *timeView;
@property (weak, nonatomic) IBOutlet UILabel *textView;

@end

@implementation XMGTopTopicView

- (IBAction)clickMore:(id)sender {
//    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"收藏",@"举报", nil];
//    [sheet showInView:self];
    
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:0];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了取消");
        
    }];
    
    [alertVc addAction:action];
    
    UIViewController *rootVc = [UIApplication sharedApplication].keyWindow.rootViewController;
    [rootVc presentViewController:alertVc animated:YES completion:nil];
    
}
+ (instancetype)viewForXib
{
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil][0];
}

- (void)setItem:(XMGTopicItem *)item
{
    _item = item;
    
    [_iconView sd_setImageWithURL:[NSURL URLWithString:item.profile_image]];
    _nameView.text = item.screen_name;
   
    _textView.text = item.text;
    
    
    // 处理时间
    
    
     _timeView.text = [self timeStr];
    
}
- (NSString *)timeStr
{
    // 2015-10-26 14:37:20
    NSString *str = _item.create_time;
    
    // 发帖日期字符串转发帖日期对象
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    // 获取发帖日期对象
    NSDate *createDate = [fmt dateFromString:str];
    
    // 获取发帖日期与当前日期时间差值
    NSDateComponents *detalCmp = [createDate detalWithNow];
    
    // 写伪代码
    if ([createDate isThisYear]) { // 今年
        
        if ([createDate isThisToday]) { // 今天
            
            if (detalCmp.hour >= 1) {
                
                str = [NSString stringWithFormat:@"%ld小时前",detalCmp.hour];
                
            } else if (detalCmp.minute >= 1) {
                
                str = [NSString stringWithFormat:@"%ld分钟前",detalCmp.minute];
                
            } else { // 刚刚
                str = @"刚刚";
            }
            
        } else if ([createDate isThisYesterday]) { // 昨天  昨天 14:30
            fmt.dateFormat = @"昨天 HH:mm";
            str = [fmt stringFromDate:createDate];
            
        } else { // 昨天之前 10-23 14:37:20
            
            fmt.dateFormat = @"MM-dd HH:mm:ss";
            str = [fmt stringFromDate:createDate];
        }
        
    }
    
    return str;
}

/*
    今年
        今天
        >1 小时
        2小时前
 
        >= 1 分钟
        1分钟前
 
        < 1分钟
        刚刚
 
        昨天
        昨天 14:30
 
        昨天之前
        10-23 14:37:20
 
    非今年
    2015-10-26 14:37:20
 */

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
